/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) Ben Foote 2008 <ben.foote@gmail.com>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <config.h>

#include <gtk/gtk.h>

#include "callbacks.h"

static void destroy(GtkWidget*, gpointer);
static void new_button(GtkWidget*, gpointer);
static void open_button(GtkWindow*, gpointer);
static void save_button(GtkWindow*, gpointer);
static void quit_button(GtkWidget*, gpointer);

int main(int argc, char *argv[])
{
	int i, j;
	GtkWidget *window, *Vbox, *Hbox, *ScrolledArea1,
			  *ScrolledArea2, *Image1, *Image2, *StatusBar;
	GtkWidget *menubar;
	GtkWidget *filemenu, *editmenu, *helpmenu;
	GtkWidget *file, *edit, *help;
	GtkWidget *new, *open, *save, *quit;
	GtkWidget *cut, *paste;
	GtkWidget *about;
	GtkWidget *separator;
	GtkWidget *table1, *table2;
	GtkWidget *Image3;
	
	GdkPixbuf *pixbuf1, *pixbuf2;
	
	GtkAccelGroup *group;
	GError *error = NULL;

	gtk_init(&argc, &argv);
	
	//top level window inits.
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window),"gtkLevel");
	//gtk_container_set_border_width(GTK_CONTAINER(window), -10);
	gtk_widget_set_size_request(window, 400, 200);
		
	//set up Menu Widgets
		group = gtk_accel_group_new();
		menubar = gtk_menu_bar_new();
		file = gtk_menu_item_new_with_label("File");
		edit = gtk_menu_item_new_with_label("Edit");
		help = gtk_menu_item_new_with_label("Help");
		filemenu = gtk_menu_new();
		editmenu = gtk_menu_new();
		helpmenu = gtk_menu_new();
		separator = gtk_separator_menu_item_new();
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit), editmenu);
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), helpmenu);
		gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
		gtk_menu_shell_append(GTK_MENU_SHELL(menubar), edit);
		gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help);
	
		open = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, group);
		new = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, group);
		save = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE, group);
		quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, group);
		gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), new);
		gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
		gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), save);
		gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), separator);
		gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
	
		cut = gtk_image_menu_item_new_from_stock(GTK_STOCK_CUT, group);
		paste = gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, group);
		gtk_menu_shell_append(GTK_MENU_SHELL(editmenu), cut);
		gtk_menu_shell_append(GTK_MENU_SHELL(editmenu), paste);
	
		about = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, group);
		gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu), about);
	
	// Set up the rest of the window
	StatusBar = gtk_statusbar_new();
	pixbuf1 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 800, 600);
	pixbuf2 = gdk_pixbuf_new_from_file("tile1.png", &error);
	
	Image1 = gtk_image_new_from_pixbuf(pixbuf2);
	Image3 = gtk_image_new_from_pixbuf(pixbuf2);
	//gtk_statusbar_push(GTK_CONTAINER(StatusBar), gtk_statusbar_get_context_id(StatusBar, "test"), "test");
	Hbox = gtk_hbox_new(FALSE, 3);
	Vbox = gtk_vbox_new(FALSE, 3);
	//Image1 = gtk_image_new_from_file("hdr.jpg");
	
	//Table 1
	table1 = gtk_table_new(10, 10, TRUE);
	gtk_table_set_row_spacings(GTK_TABLE(table1), 1);
	gtk_table_set_col_spacings(GTK_TABLE(table1), 1);	

	gtk_table_attach(GTK_TABLE(table1), GTK_WIDGET(Image1), 0, 1, 0, 1,
					 GTK_EXPAND, GTK_SHRINK, 0, 0);
	
	gtk_table_attach(GTK_TABLE(table1), GTK_WIDGET(Image3), 1, 2, 1, 2,
					 GTK_EXPAND, GTK_SHRINK, 0, 0);
	
//	for(i=0;i<10;i++)
//	{
//		for(j=0;j<10;j++)
//		{
//			gtk_table_attach(GTK_TABLE(table1), GTK_WIDGET(Image1), i, i+1, j ,j+1,
//							 GTK_EXPAND, GTK_SHRINK, 0, 0);
//		}
//	}
	
	//Image1 = gtk_image_new_from_pixbuf(pixbuf1);
	Image2 = gtk_image_new_from_file("hdr-landscape.jpg");
	ScrolledArea1 = gtk_scrolled_window_new(NULL, NULL);
	ScrolledArea2 = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ScrolledArea1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ScrolledArea2), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ScrolledArea1), Image2);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ScrolledArea2), table1);
	
	gtk_box_pack_start_defaults(GTK_BOX(Hbox), ScrolledArea1);
	gtk_box_pack_start_defaults(GTK_BOX(Hbox), ScrolledArea2);
	
	gtk_box_pack_start(GTK_BOX(Vbox), menubar, FALSE, FALSE, 2);
	gtk_box_pack_start(GTK_BOX(Vbox), Hbox, TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX(Vbox), StatusBar, FALSE, FALSE, 2);
	
	//connect signals
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
	g_signal_connect(G_OBJECT(new),  "button-press-event", G_CALLBACK(new_button), NULL);
	g_signal_connect(G_OBJECT(open), "button-press-event", G_CALLBACK(open_button), NULL);
	g_signal_connect(G_OBJECT(save), "button-press-event", G_CALLBACK(save_button), NULL);
	g_signal_connect(G_OBJECT(quit), "button-press-event", G_CALLBACK(quit_button), NULL);
	
	gtk_container_add(GTK_CONTAINER(window), Vbox);
	gtk_window_add_accel_group(GTK_WINDOW(window), group);
	gtk_widget_show_all(window);
	
	gtk_main();	
	return 0;
}

static void destroy(GtkWidget *window, gpointer data)
{
	gtk_main_quit();
}

static void new_button(GtkWidget *window, gpointer data)
{
	GtkWidget* dialog;
	char *filename;
	
	dialog = gtk_file_chooser_dialog_new("New File...", NULL,
										 GTK_FILE_CHOOSER_ACTION_SAVE,
										 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
										 GTK_STOCK_NEW, GTK_RESPONSE_ACCEPT,
										 NULL);
	
	gint result = gtk_dialog_run(GTK_DIALOG(dialog));
	
	if(result == GTK_RESPONSE_ACCEPT)
	{
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		g_print("Filename new: %s\n", filename);
	}
	
	gtk_widget_destroy(dialog);
}

static void open_button(GtkWindow* window, gpointer data)
{
	GtkWidget* dialog;
	char *filename;
	
	dialog = gtk_file_chooser_dialog_new("Open File...", NULL,
										 GTK_FILE_CHOOSER_ACTION_OPEN,
										 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
										 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
										 NULL);
	
	gint result = gtk_dialog_run(GTK_DIALOG(dialog));
	
	if(result == GTK_RESPONSE_ACCEPT)
	{
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		g_print("Filename selected: %s\n", filename);
	}
	
	gtk_widget_destroy(dialog);
}

static void save_button(GtkWindow *window, gpointer data)
{
	GtkWidget *dialog;
	char *filename;
	
	dialog = gtk_file_chooser_dialog_new("Save File...", NULL,
										 GTK_FILE_CHOOSER_ACTION_SAVE,
										 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
										 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
										 NULL);
	
	gint result = gtk_dialog_run(GTK_DIALOG(dialog));
	
	if(result == GTK_RESPONSE_ACCEPT)
	{
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		g_print("Filename selected: %s\n", filename);
	}
	
	gtk_widget_destroy(dialog);
}

static void quit_button(GtkWidget *window, gpointer data)
{
	gtk_main_quit();
}
